package org.andcool.ui.activity;

import org.andcool.audio.music.MusicManager;
import org.andcool.audio.sound.SoundManager;
import org.andcool.sensor.location.ILocationListener;
import org.andcool.sensor.location.LocationProviderStatus;
import org.andcool.sensor.location.LocationSensorOptions;
import org.andcool.util.ActivityUtils;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Vibrator;

/**
 * @author Joosun
 * @since 2011-5-6
 */
public class BaseActivity extends Activity implements LocationListener
{
	private Vibrator mVibrator;

	private SoundManager mSoundManager;
	private MusicManager mMusicManager;

	private Location mLocation;
	private ILocationListener mLocationListener;

	public void requestNoTitlescreen()
	{
		ActivityUtils.requestNoTitlescreen(this);
	}

	public void requestFullscreen()
	{
		ActivityUtils.requestFullscreen(this);
	}

	public void setScreenBrightness(final float pScreenBrightness)
	{
		ActivityUtils.setScreenBrightness(this, pScreenBrightness);
	}

	public void keepScreenOn()
	{
		ActivityUtils.keepScreenOn(this);
	}

	public boolean enableVibrator()
	{
		this.mVibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);

		return this.mVibrator != null;
	}

	public void vibrate(final long pMilliseconds) throws IllegalStateException
	{
		if(this.mVibrator != null)
		{
			this.mVibrator.vibrate(pMilliseconds);
		}
		else
		{
			throw new IllegalStateException("You need to enable the Vibrator before you can use it!");
		}
	}

	public void vibrate(final long[] pPattern, final int pRepeat) throws IllegalStateException
	{
		if(this.mVibrator != null)
		{
			this.mVibrator.vibrate(pPattern, pRepeat);
		}
		else
		{
			throw new IllegalStateException("You need to enable the Vibrator before you can use it!");
		}
	}

	public void enableSound()
	{
		if (this.mSoundManager == null)
		{
			this.mSoundManager = new SoundManager();
		}
	}

	public void enableSound(final int pMaxSimultaneousStreams)
	{
		if (this.mSoundManager == null)
		{
			this.mSoundManager = new SoundManager(pMaxSimultaneousStreams);
		}
	}

	public SoundManager getSoundManager()
	{
		if (this.mSoundManager != null)
		{
			return this.mSoundManager;
		}
		else
		{
			throw new IllegalStateException("You need to enable the Sound before you can use it!");
		}
	}

	public void enableMusic()
	{
		if (this.mMusicManager == null)
		{
			this.mMusicManager = new MusicManager();
		}
	}

	public MusicManager getMusicManager()
	{
		if (this.mMusicManager != null)
		{
			return this.mMusicManager;
		}
		else
		{
			throw new IllegalStateException("You need to enable the Music before you can use it!");
		}
	}

	public void enableLocationSensor(final ILocationListener pLocationListener, final LocationSensorOptions pLocationSensorOptions)
	{
		this.mLocationListener = pLocationListener;

		final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

		final String locationProvider = locationManager.getBestProvider(pLocationSensorOptions, pLocationSensorOptions.isEnabledOnly());

		locationManager.requestLocationUpdates(locationProvider, pLocationSensorOptions.getMinimumTriggerTime(), pLocationSensorOptions.getMinimumTriggerDistance(), this);

		this.onLocationChanged(locationManager.getLastKnownLocation(locationProvider));
	}

	public void disableLocationSensor()
	{
		final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		locationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(final Location pLocation)
	{
		if (this.mLocation == null)
		{
			this.mLocation = pLocation;
		}
		else
		{
			if (pLocation == null)
			{
				this.mLocationListener.doLocationLost();
			}
			else
			{
				this.mLocation = pLocation;
				this.mLocationListener.doLocationChanged(pLocation);
			}
		}
	}

	@Override
	public void onProviderDisabled(final String pProvider)
	{
		this.mLocationListener.doLocationProviderDisabled();
	}

	@Override
	public void onProviderEnabled(final String pProvider)
	{
		this.mLocationListener.doLocationProviderEnabled();
	}

	@Override
	public void onStatusChanged(final String pProvider, final int pStatus, final Bundle pExtras)
	{
		switch (pStatus)
		{
			case LocationProvider.AVAILABLE:
				this.mLocationListener.doLocationProviderStatusChanged(LocationProviderStatus.AVAILABLE, pExtras);
				break;
			case LocationProvider.OUT_OF_SERVICE:
				this.mLocationListener.doLocationProviderStatusChanged(LocationProviderStatus.OUT_OF_SERVICE, pExtras);
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				this.mLocationListener.doLocationProviderStatusChanged(LocationProviderStatus.TEMPORARILY_UNAVAILABLE, pExtras);
				break;

			default:
				break;
		}
	}
}